package leetcode.set_map;

import java.util.HashMap;

/**
 * 面试题 16.02. 单词频率
 * <p>
 * 设计一个方法，找出任意指定单词在一本书中的出现频率。
 * <p>
 * 你的实现应该支持如下操作：
 * <p>
 * WordsFrequency(book)构造函数，参数为字符串数组构成的一本书
 * get(word)查询指定单词在书中出现的频率
 * <p>
 * 示例：
 * <p>
 * WordsFrequency wordsFrequency = new WordsFrequency({"i", "have", "an", "apple", "he", "have", "a", "pen"});
 * wordsFrequency.get("you"); //返回0，"you"没有出现过
 * wordsFrequency.get("have"); //返回2，"have"出现2次
 * wordsFrequency.get("an"); //返回1
 * wordsFrequency.get("apple"); //返回1
 * wordsFrequency.get("pen"); //返回1
 * <p>
 * 提示：
 * <p>
 * book[i]中只包含小写字母
 * 1 <= book.length <= 100000
 * 1 <= book[i].length <= 10
 * get函数的调用次数不会超过100000
 */
public class Solution16_02 {
    private static HashMap<String, Integer> freq;

    class WordsFrequency {


        public WordsFrequency(String[] book) {
            freq = new HashMap<>(100000);
            for (int i = 0; i < book.length; i++) {
                String key = book[i];
                if (!freq.containsKey(key)) {
                    freq.put(key, 1);
                } else {
                    freq.put(key, freq.get(key) + 1);
                }

            }
        }

        public int get(String word) {
            if (!freq.containsKey(word)) {
                return 0;
            }
            return freq.get(word);
        }
    }

}
